<?php
/**
 * FileImage.php - 18 Dec 2008
 * 
 * @author Kevin Breed
 */

require_once( 'KB/Image.php' );

class KB_Image_File extends KB_Image
{
	/**
	 * @var string	The full path to the file
	 */
	public $pathName;
	
	/**
	 * @var string	The path to the file without baseName
	 */
	protected $_path;
	
	/**
	 * @var string	The name of the file without path
	 */
	protected $_baseName;
	
	/**
	 * @var string	The name of the file without path and extension
	 */
	protected $_fileName;
	
	/**
	 * @var string	The extension of the file
	 */
	protected $_extension;
	
	/**
	 * @var bool	Indication whether the properties that are obtained throug
	 * 				pathinfo() are set or not.
	 */
	protected $_pathInfoPropsSetFlag = false;
	
	/**
	 * @var int		The imagetype (imagetype_XXX) as a constant
	 */
	public $type;
	
	/**
	 * @var string	The mimetype of the image
	 */
	public $mimetype;
	
	
	/**
	 * @var array	Imagetypes that pass as valid image files
	 */
	protected $validImageTypes = array();
	
	##############
	# EXCEPTIONS #
	##############
	
	/**
	 * @var int		Exception code: invalid imagetype
	 */
	public static $E_INVALID_IMAGETYPE = 1;
	
	#####################
	# GETTERS & SETTERS #
	####################
	
	public function getPath ()
	{
		if ( !$this->_pathInfoPropsSetFlag ) $this->_setInfoProps();
		
		return $this->_path;
	}
	
	public function getBaseName ()
	{
		if ( !$this->_pathInfoPropsSetFlag ) $this->_setInfoProps();
	
		return $this->_baseName;
	}
	
	public function getFileName ()
	{
		if ( !$this->_pathInfoPropsSetFlag ) $this->_setInfoProps();
	
		return $this->_fileName;
	}
	
	public function getExtension ()
	{
		if ( !$this->_pathInfoPropsSetFlag ) $this->_setInfoProps();
	
		return $this->_extension;
	}
	
	/**
	 * CONSTRUCTOR
	 * 
	 * @param	string	$pathName	The full path to the file
	 * @return	void;
	 */
	public function __construct ( $pathName )
	{
		$this->pathName = $pathName;
		
		if ( !exif_imagetype( $pathName ) )
		{
			throw new Exception( "The given file '$pathName' is not a valid " .
								 "image type.", self::$E_INVALID_IMAGETYPE );
			return;
		}
	}
	
	/**
	 * Sets the properties of this object which are obtained through pathinfo()
	 * 
	 * @return void;
	 */
	protected function _setInfoProps()
	{
		$pathInfo 			= pathinfo( $this->pathName );
		
		$this->_path		= $pathInfo[ 'dirname' ]; 
		$this->_baseName 	= $pathInfo[ 'basename' ]; 
		$this->_fileName 	= $pathInfo[ 'filename' ]; 
		$this->_extension	= $pathInfo[ 'extension' ];
		
		$this->_pathInfoPropsSetFlag = true;
	}
}

?>